package com.mobibom.pipelines;

import pipelines.free.beta.R;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class ChooseLevelActivity extends Activity {
	DatabaseHelper db;
	Button[] buttonArray;
	
	Button[] buttonArray2 = new Button[52];
	String[] levels;
	
	int[] topscores;
	int pack;
	private AdView adView1;
	private static final String MY_BANNER_UNIT_ID = "a14ed7bde839b79";
	
	LinearLayout verticalLayout;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.chooselevelview);
	    
	    setViews();
	    db = new DatabaseHelper(this);
	    
	    Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
        	pack = extras.getInt("levelpack");
        	String[] allLevels = new String[db.AANTALLEVELS];
        	allLevels = db.getAllLevels(pack);
        	topscores =db.getAllTopscores(pack);
        	
        	
        	int notEmpty = 0;
        	for(int i = 0; i < allLevels.length;i++){
        		if( allLevels[i] != null){
        			notEmpty++;
        		}
        	}
        	levels = new String[notEmpty];
        	int count = 0;
        	for(int i = 0; i < allLevels.length;i++){
        		if( allLevels[i] != null){
        			levels[count] = allLevels[i];
        			count++;
        		}
        	}
	    
		    
		    Display display = getWindowManager().getDefaultDisplay(); 
			int screenwidth = ((display.getWidth()-50)/4);
		    
		    for(int i = 0; i < levels.length; i++){
		    	
		    	buttonArray2[i].setHeight(screenwidth);
		    	buttonArray2[i].setWidth(screenwidth);
		    	buttonArray2[i].setEnabled(false);
		    //	buttonArray2[i].setEnabled(true);
		     	
		    	
		     	int aantalSterren = checkSterren(topscores[i]); 
		    	
		        if(pack == 1){
		    		String[] l = levels[i].split("1x");
		    		buttonArray2[i].setText( l[1] ); 
		    	      	//western
		    	      			// 3 sterren
		    	      			if(aantalSterren == 3 ){
		    	      				buttonArray2[i].setBackgroundResource(R.drawable.western3);
		    	      			}
		    	      			// 2 sterren
		    	      			else if(aantalSterren == 2){
		    	      				buttonArray2[i].setBackgroundResource(R.drawable.western2);
		    	      			}
		    	      			// 1 sterren
		    	      			else if(aantalSterren == 1){
		    	      				buttonArray2[i].setBackgroundResource(R.drawable.western1);;
		    	      			}
		    	      			// 0 sterren
		    	      			else{
		    	      				buttonArray2[i].setBackgroundResource(R.drawable.westernbutton0sterren);
		    	      			}
		    	      		//alien
		        }
		        else if(pack == 2){
		        	String[] l = levels[i].split("2x");
		        	buttonArray2[i].setText( l[1] );
    	      			// 3 sterren
    	      			if(aantalSterren == 3 ){
    	      				buttonArray2[i].setBackgroundResource(R.drawable.alien3);
    	      			}
    	      			// 2 sterren
    	      			else if(aantalSterren == 2){
    	      				buttonArray2[i].setBackgroundResource(R.drawable.alien2);
    	      			}
    	      			// 1 sterren
    	      			else if(aantalSterren == 1){
    	      				buttonArray2[i].setBackgroundResource(R.drawable.alien1);
    	      			}
    	      			// 0 sterren
    	      			else{
    	      				buttonArray2[i].setBackgroundResource(R.drawable.alienbutton0sterren);
    	      			}
		        }
		        else if(pack == 3){
		        	String[] l = levels[i].split("3x");
		        	buttonArray2[i].setText( l[1] ); 
		        	
		        	if(aantalSterren == 3 ){
	      				buttonArray2[i].setBackgroundResource(R.drawable.bier3sterren);
	      			}
	      			// 2 sterren
	      			else if(aantalSterren == 2){
	      				buttonArray2[i].setBackgroundResource(R.drawable.bier2sterren);
	      			}
	      			// 1 sterren
	      			else if(aantalSterren == 1){
	      				buttonArray2[i].setBackgroundResource(R.drawable.bier1sterren);
	      			}
	      			// 0 sterren
	      			else{
	      				buttonArray2[i].setBackgroundResource(R.drawable.beerbutton0sterren);
	      			}
		        }
		        
		        
		    }
		    SharedPreferences settings = getSharedPreferences("levelsInDB", Context.MODE_PRIVATE);
		    if(pack == 1){
		    	int levelsUitgespeeld = settings.getInt("wereld1level", 1);
		    	for(int i = 0; i< levelsUitgespeeld; i++){
			    	buttonArray2[i].setEnabled(true);
			    }
		    }//level pack 2 TODO debug
		    else if (pack == 2){
		    		int levelsUitgespeeld = settings.getInt("wereld2level", 1);
			    	for(int i = 0; i< levelsUitgespeeld; i++){
				    	buttonArray2[i].setEnabled(true);
				    }
			    }
			    else if (pack == 3){
		    		int levelsUitgespeeld = settings.getInt("wereld3level", 1);
			    	for(int i = 0; i< levelsUitgespeeld; i++){
				    	buttonArray2[i].setEnabled(true); //TODO
				    }
			    }
		}
	    addOnClickListeners();
	    
	}

	private int checkSterren(int sec ){
		int aantalSterren = 0;
		
		//western
		if(pack == 1){
			// 3 sterren
			if(sec == 0){
				aantalSterren = 0;
			}
			else  if(sec < 30 ){
				aantalSterren = 3;
			}
			// 2 sterren
			else if(sec < 60){
				aantalSterren = 2;
			}
			// 1 sterren
			else if(sec < 90){
				aantalSterren = 1;
			}
			// 0 sterren
			else{
				aantalSterren = 0;
			}
		}
		//alien
		else if(pack == 2){
			// 3 sterren
			if(sec == 0){
				aantalSterren = 0;
			}
			else if(sec < 60 ){
				aantalSterren = 3;
			}
			// 2 sterren
			else if(sec < 90){
				aantalSterren = 2;
			}
			// 1 sterren
			else if(sec < 120){
				aantalSterren = 1;
			}
			// 0 sterren
			else{
				aantalSterren = 0;
			}
		}//bier
		else if(pack == 3){
			if(sec == 0){
				aantalSterren = 0;
			}
			// 3 sterren
			else if(sec < 70 ){
				aantalSterren = 3;
			}
			// 2 sterren
			else if(sec < 100){
				aantalSterren = 2;
			}
			// 1 sterren
			else if(sec < 130){
				aantalSterren = 1;
			}
			// 0 sterren
			else{
				aantalSterren = 0;
			}
		}
		
			
		return aantalSterren;
	}
	
	
	private void setViews() {
		verticalLayout = 	(LinearLayout)findViewById(R.id.linearLayoutLevels);
		// Lookup R.layout.main
	    LinearLayout layout = (LinearLayout)findViewById(R.id.layoutAdsChoseLevel);
	    // Create the adView
	    // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
	    adView1 = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
	    // Add the adView to it
	    layout.addView(adView1);
	    // Initiate a generic request to load it with an ad
	    AdRequest request = new AdRequest();
	    //request.setTesting(true);
	    adView1.loadAd(request);   
	    
	    
	    //set Buttons:
	    buttonArray2[0] =  (Button)findViewById(R.id.button1);
	    buttonArray2[1] =  (Button)findViewById(R.id.button2);
	    buttonArray2[2] =  (Button)findViewById(R.id.button3);
	    buttonArray2[3] =  (Button)findViewById(R.id.button4);
	    
	    buttonArray2[4] =  (Button)findViewById(R.id.button5);
	    buttonArray2[5] =  (Button)findViewById(R.id.button6);
	    buttonArray2[6] =  (Button)findViewById(R.id.button7);
	    buttonArray2[7] =  (Button)findViewById(R.id.button8);
	    
	    buttonArray2[8] =  (Button)findViewById(R.id.button9);
	    buttonArray2[9] =  (Button)findViewById(R.id.button10);
	    buttonArray2[10] =  (Button)findViewById(R.id.button11);
	    buttonArray2[11] =  (Button)findViewById(R.id.button12);
	    
	    buttonArray2[12] =  (Button)findViewById(R.id.button13);
	    buttonArray2[13] =  (Button)findViewById(R.id.button14);
	    buttonArray2[14] =  (Button)findViewById(R.id.button15);
	    buttonArray2[15] =  (Button)findViewById(R.id.button16);
	    
	    buttonArray2[16] =  (Button)findViewById(R.id.button17);
	    buttonArray2[17] =  (Button)findViewById(R.id.button18);
	    buttonArray2[18] =  (Button)findViewById(R.id.button19);
	    buttonArray2[19] =  (Button)findViewById(R.id.button20);
	    
	    buttonArray2[20] =  (Button)findViewById(R.id.button21);
	    buttonArray2[21] =  (Button)findViewById(R.id.button22);
	    buttonArray2[22] =  (Button)findViewById(R.id.button23);
	    buttonArray2[23] =  (Button)findViewById(R.id.button24);
	    
	    buttonArray2[24] =  (Button)findViewById(R.id.button25);
	    buttonArray2[25] =  (Button)findViewById(R.id.button26);
	    buttonArray2[26] =  (Button)findViewById(R.id.button27);
	    buttonArray2[27] =  (Button)findViewById(R.id.button28);
	    
	    buttonArray2[28] =  (Button)findViewById(R.id.button29);
	    buttonArray2[29] =  (Button)findViewById(R.id.button30);
	    buttonArray2[30] =  (Button)findViewById(R.id.button31);
	    buttonArray2[31] =  (Button)findViewById(R.id.button32);
	    
	    buttonArray2[32] =  (Button)findViewById(R.id.button33);
	    buttonArray2[33] =  (Button)findViewById(R.id.button34);
	    buttonArray2[34] =  (Button)findViewById(R.id.button35);
	    buttonArray2[35] =  (Button)findViewById(R.id.button36);
	    
	    buttonArray2[36] =  (Button)findViewById(R.id.button37);
	    buttonArray2[37] =  (Button)findViewById(R.id.button38);
	    buttonArray2[38] =  (Button)findViewById(R.id.button39);
	    buttonArray2[39] =  (Button)findViewById(R.id.button40);
	    
	    buttonArray2[40] =  (Button)findViewById(R.id.button41);
	    buttonArray2[41] =  (Button)findViewById(R.id.button42);
	    buttonArray2[42] =  (Button)findViewById(R.id.button43);
	    buttonArray2[43] =  (Button)findViewById(R.id.button44);
	    
	    buttonArray2[44] =  (Button)findViewById(R.id.button45);
	    buttonArray2[45] =  (Button)findViewById(R.id.button46);
	    buttonArray2[46] =  (Button)findViewById(R.id.button47);
	    buttonArray2[47] =  (Button)findViewById(R.id.button48);
	    
	    buttonArray2[48] =  (Button)findViewById(R.id.button49);
	    buttonArray2[49] =  (Button)findViewById(R.id.button50);
	    buttonArray2[50] =  (Button)findViewById(R.id.button51);
	    buttonArray2[51] =  (Button)findViewById(R.id.button52);
	}
	
	private void addOnClickListeners() {
		buttonArray2[0].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(1);}});  
	    buttonArray2[1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(2);}});  
	    buttonArray2[2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(3);}});    
	    buttonArray2[3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(4);}});   
	    
	    buttonArray2[4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(5);}});  
	    buttonArray2[5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(6);}});  
	    buttonArray2[6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(7);}});  
	    buttonArray2[7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(8);}});  
	    
	    buttonArray2[8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(9);}});  
	    buttonArray2[9].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(10);}});  
	    buttonArray2[10].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(11);}});  
	    buttonArray2[11].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(12);}});  
	    
	    buttonArray2[12].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(13);}});  
	    buttonArray2[13].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(14);}});  
	    buttonArray2[14].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(15);}});  
	    buttonArray2[15].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(16);}});  
	    
	    buttonArray2[16].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(17);}});  
	    buttonArray2[17].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(18);}});  
	    buttonArray2[18].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(19);}});  
	    buttonArray2[19].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(20);}});  
	    
	    buttonArray2[20].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(21);}});  
	    buttonArray2[21].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(22);}});  
	    buttonArray2[22].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(23);}});  
	    buttonArray2[23].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(24);}});  
	    
	    buttonArray2[24].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(25);}});  
	    buttonArray2[25].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(26);}});  
	    buttonArray2[26].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(27);}});  
	    buttonArray2[27].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(28);}});  
	    
	    buttonArray2[28].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(29);}});  
	    buttonArray2[29].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(30);}});  
	    buttonArray2[30].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(31);}});  
	    buttonArray2[31].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(32);}});  
	    
	    buttonArray2[32].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(33);}});  
	    buttonArray2[33].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(34);}});  
	    buttonArray2[34].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(35);}});  
	    buttonArray2[35].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(36);}});  
	    
	    buttonArray2[36].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(37);}});  
	    buttonArray2[37].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(38);}});  
	    buttonArray2[38].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(39);}});  
	    buttonArray2[39].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(40);}});  
	    
	    buttonArray2[40].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(41);}});  
	    buttonArray2[41].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(42);}});  
	    buttonArray2[42].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(43);}});  
	    buttonArray2[43].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(44);}});  
	    
	    buttonArray2[44].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(45);}});  
	    buttonArray2[45].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(46);}});  
	    buttonArray2[46].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(47);}});  
	    buttonArray2[47].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(48);}});  
	    
	    buttonArray2[48].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(49);}});  
	    buttonArray2[49].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(50);}});  
	    buttonArray2[50].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(51);}});  
	    buttonArray2[51].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {openLevel(52);}});  
		
	}

	private void openLevel(int i){
		//open level
		
		db.close();
		Intent level = new Intent(getApplicationContext(), PipelinesActivity.class);
		level.putExtra("level", levels[i-1]);
		level.putExtra("levelpack", pack);
		startActivity(level);
		this.finish();
	}
	
}
